#include "projHeader.h"

traffic::traffic(void)
{
	X = 1;
	Y = 1;
	able = false;
	tillGreen = 15;
	tillRed = 15;
	curTime = 0;
	author = new char [5];
	strcpy (author, "none");
}

traffic::traffic (int X, int Y, int tillGreen, int tillRed, bool able, char* author)
{
	if ((X<1) || (X>maxX)) X=1;
	if ((Y<1) || (Y>maxY)) Y=1;
	if (tillGreen < 1) tillGreen = 15;
	if (tillRed < 1) tillRed = 15;
	curTime = 0;
	this->X = X;
	this->Y = Y;
	this->tillGreen = tillGreen;
	this->tillRed = tillRed;
	this->able = able;
	if (!strlen(author))
	{
		this->author = new char [5];
		strcpy (this->author, "none");
	}
	else
	{
		this->author = new char [strlen(author)+1];
		strcpy (this->author, author);
	}
}

void traffic::setData (int X, int Y, int tillGreen, int tillRed, bool able, char* author)
{
	if ((X<1) || (X>maxX)) X=1;
	if ((Y<1) || (Y>maxY)) Y=1;
	if (tillGreen < 1) tillGreen = 15;
	if (tillRed < 1) tillRed = 15;
	curTime = 0;
	this->X = X;
	this->Y = Y;
	this->tillGreen = tillGreen;
	this->tillRed = tillRed;
	this->able = able;
	delete [] this->author;
	if (!strlen(author))
	{
		this->author = new char [5];
		strcpy (this->author, "none");

	}
	else
	{
		this->author = new char [strlen(author)+1];
		strcpy (this->author, author);
	}

}

void traffic::show (mapElt** MAP)
{
	curTime+=1;
	if (((!able) && (curTime == tillGreen)) || ((able) && (curTime == tillRed)))
	{
		able = !able;
		curTime = 0;
	}
	int color;
	if (able) color = 138; //dark_gray font
	else if (curTime > tillGreen-3) color = 142;//dark_gray font
	else color = 140;//dark_gray font
	//COORD c;
	//c.X = X;
	//c.Y = Y;
	//SetConsoleCursorPosition(h,c);
	//SetConsoleTextAttribute(h,color);
	MAP[X][Y].setAll ('*', color, author, "Traffic Light");
	//cout << "*";
}

traffic::~traffic(void)
{
	delete [] author;
}
